package com.ruoyi.common.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StringMD5Checksum {

    public static String getStringMD5Checksum(String input) {
        try {
            // 创建一个MessageDigest实例，指定使用MD5算法
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 将字符串转换为字节数组，并计算哈希值
            byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

            // 将哈希值转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashBytes) {
                sb.append(String.format("%02x", b));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    public static void main(String[] args) {
        String input = "yourStringHere"; // 替换为你想计算MD5哈希值的字符串
        String md5Checksum = getStringMD5Checksum(input);
        System.out.println("MD5 Checksum: " + md5Checksum);
    }
}